<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:''}" :title="xheader.title" class="XHeader"></XHeader>
    </div>
    <div class="pagetop">
      <div class="topbar block">
        <!--        <form @submit="onSoso($event)" class="inputbox flex flex-center">-->
        <div class="inputbox flex flex-center">
          <chimage class="icon" mode="aspectFit" :src="require('@/assets/img/ic_search.png')"></chimage>
          <input
            type="search"
            focus="true"
            style="text-align: left"
            v-model="goods_name"
            class="input flex-full"
            placeholder="搜索商品"
            @keyup.enter="onSoso(searchParams)"
          >
          <div class="searchSubmit" @click="onSoso(searchParams)">搜索</div>
        </div>
      </div>
    </div>

    <!--筛选排序-->
    <screen-item v-model="searchParams" @onSoso="onSoso" v-if="isScreen" :cateGoodsCount="cateGoodsCount"
                 :supplierList="supplierList"/>

    <scroller
      @on-pullup-loading="soso(searchParams)"
      lock-x
      ref="scroller"
      :pullup-config="{content:'',upContent:'',downContent:'释放加载更多',loadingContent:'加载中...',}"
      height="-45"
      :use-pullup="true"
      style=" padding-bottom:72px;"
      v-model="status"
    >
      <div class="page">
        <div class="block historybox" v-if="isHistorybox">
          <div class="title">历史搜索</div>
          <div class="historybar">
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
            <div class="item">商品</div>
          </div>
        </div>

        <div class="block" style="margin-top:12px;">
          <div class="grid grid-gutter discountbox">
            <div class="grid-1-2" v-for="(item,inedx) in items" :key="inedx">
              <div class="item" :class="[isHasGoodsText(item) ? 'noStock' : '']" @click="onInfo(item)">
                <div class="noStock-back" v-if="isHasGoodsText(item)"></div>
                <div class="noStock-con">
                  <chimage class="image" :src="item.goods_image_main" mode="aspectFill"></chimage>
                  <div class="noStock-tip" v-if="isHasGoodsText(item)">
                    <div class="noStock-tip-text">无货</div>
                  </div>
                </div>
                <div class="title">{{item.goods_name}}</div>
                <!--<div class="price">{{item.internal_price}}</div>-->
                <div class="list-price">
                  <div class="price newPrice">{{textNewPrice}}￥{{item.internal_price}}</div>
                  <div v-if="!isHasGoodsText(item)" class="shop-btn"
                       @click.stop="onAddToCart(item.goods_sku, item.goods_moq)">
                    <img :src="require('@/assets/img/icon_item_gwc.png')"/>
                  </div>
                </div>

                <div class="list-price">
                  <div class="price oldPrice">{{textOldPrice(item.supplier_id)}}￥{{item.reference_price}}</div>
                </div>

                <div class="list-price">
                  <div v-if="discountClass(item.discount_rate)" class="discount"
                       :class="discountClass(item.discount_rate)">
                    <chimage class="icon" :src="require('@/assets/img/icon_discount.png')"></chimage>
                    <span>{{item.discount_rate}}%</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <load-more
          v-if="!more&&!items.length"
          :show-loading="false"
          :tip="'暂无数据'"
          background-color="#fbf9fe"
        ></load-more>
      </div>
    </scroller>
  </div>
</template>

<script>
  import Mall from '@/dll/mall'
  import shop from '@/mixin/shop'
  import screen from '@/mixin/screen'
  import ScreenItem from '../../../components/screen'

  export default {
    name: 'mallSearch',
    mixins: [shop, screen],
    components: {
      ScreenItem
    },
    data () {
      return {
        xheader: {
          title: '搜索'
        },
        items: [],
        isHistorybox: false,
        loading: false,
        more: true,
        so: {
          limit: 20,
          page: 1
        },
        goods_name: '',
        status: {
          pullupStatus: 'default'
        },
        quota_id: this.$route.query.quota_id,

        // 筛选
        cateGoodsCount: [],
        supplierList: [],
        isScreen: false,
        cate_id: this.$route.query.cateId || undefined, // 分类id
        searchParams: {}
      }
    },
    created () {
      this.init()
    },
    mounted () {
      this.onHidden()
    },
    watch: {
      // defaultAddress (val, oldVal) {
      //   if (oldVal && oldVal !== val) {
      //     this.onSoso(this.searchParams)
      //   }
      // }
    },
    methods: {
      init () {
        let so = JSON.parse(this.$route.query.so || '{}')
        let title = this.$route.query.title
        if (so) {
          this.so = Object.assign(this.so, so)
        }

        if (title) {
          this.xheader.title = title
        }

        if (this.$route.query.load) {
          this.soso()
        }
        if (this.$route.query.quota_id) {
          this.soso()
        }
      },
      onInfo (item) {
        this.onAuto()
        this.$router.push({
          name: 'mallInfo',
          params: { id: item.goods_sku || 0 }
        })
      },
      onSoso (searchParams = {}) {
        // e.preventDefault()
        this.so.page = 1
        this.more = true
        this.items = []
        this.images = {}
        this.status.pullupStatus = 'disabled'
        this.$nextTick(function () {
          this.$refs.scroller.enablePullup()
          this.$refs.scroller.reset({
            top: 0
          })
          this.status.pullupStatus = 'default'
        })
        this.soso(searchParams)
      },
      async soso (searchParams = {}) {
        let param = {}
        param = Object.assign({}, param, this.so)
        if (!this.more) {
          return
        }
        // eslint-disable-next-line no-unused-expressions
        this.goods_name ? param.keyword = this.goods_name : undefined
        // eslint-disable-next-line no-unused-expressions
        this.cate_id ? param.cate_id = this.cate_id : undefined
        // eslint-disable-next-line no-unused-expressions
        this.quota_id ? param.quota_id = this.quota_id : undefined
        this.$vux.loading.show({
          text: '加载中'
        })
        param = Object.assign({
          sort_type: 'discount',
          sort_desc: 1
        }, { area_code: this.defaultAddress.code }, param, searchParams)

        let res = await Mall.searchGoods(param)
        this.$vux.loading.hide()
        if (res.code == 0) {
          this.isScreen = true
          this.cateGoodsCount = res.data.cate_goods_count
          this.supplierList = res.data.supplier_list
          if (param.page >= res.data.page_total) {
            this.more = false
            this.$refs.scroller.disablePullup()
          } else {
            this.$refs.scroller.donePullup()
          }

          let items = [].concat(this.items, res.data.list)
          this.items = items.filter((item) => { // 过滤掉无货商品
            return parseFloat(item.stock_state) !== -1
          })

          // this.items = [].concat(this.items, res.data.list)
          // param.page = param.page + 1
          this.so.page = this.so.page + 1
          if (!this.items.length) this.more = false
        } else {
          this.more = false
          this.$refs.scroller.disablePullup()
          this.$vux.alert.show({
            title: '提示',
            content: res.msg
          })
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  /deep/ .pageheader {
    z-index: 2 !important;
  }

  .page {
    padding-top: 84px;
  }

  .pagetop {
    display: block;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 2;
    margin-top: 45px !important;
  }

  .topbar {
    box-sizing: border-box;
    background-color: #fff;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .topbar .icon {
    width: 20px;
    height: 20px;
    padding: 0 5px;
    display: block;
  }

  .topbar .inputbox {
    background: #fbf8f7;
    height: 35px;
  }

  .searchSubmit {
    height: 35px;
    line-height: 35px;
    font-size: 12px;
    border: none;
    background-color: transparent;
    color: #351009;
    width: 60px;
    text-align: center;
  }

  .topbar .input {
    font-size: 12px;
    color: #351009;
    text-align: center;
    line-height: 17px;
  }

  .historybox {
    position: fixed;
    width: 100%;
    top: 45px;
    left: 0;
  }

  .historybox .title {
    line-height: 20px;
    font-size: 14px;
    color: #351009;
  }

  .historybox .historybar {
    display: flex;
    flex-wrap: wrap;
    margin-left: -20px;
  }

  .historybox .historybar .item {
    display: block;
    height: 28px;
    font-size: 14px;
    color: #5b5b5b;
    line-height: 28px;
    padding-left: 34px;
    padding-right: 34px;
    background-color: #ffffff;
    border-radius: 14px;
    margin-left: 16px;
    margin-top: 16px;
  }

  .discountbox .item {
    display: block;
    background-color: #fff;
    padding: 12px;
    box-sizing: border-box;
  }

  .discountbox .item .image {
    display: block;
    height: 145px;
    width: 100%;
    background-color: #fbf8f7;
    background-size: cover;
  }

  .discountbox .item > .title {
    margin-top: 16px;
    font-size: 12px;
    color: #575757;
    line-height: 17px;
    height: 34px;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .discountbox .item > .price {
    font-size: 14px;
    color: #ae2309;
    line-height: 16px;
  }
</style>
